/*
    Events.cpp

    Date:
        05-09-10

    Descripton:
        An object to store 12 hours of events from a calendar (Items)

    Authors:
        Nathaniel Brown
        Subodh Prabhu
        Jesus Suarez
*/


/****************************************************************************/
/*                              I N C L U D E S                             */
/****************************************************************************/

#include "GlutHeaders.h"
#include "Events.h"
#include "Calendar.h"
#include "Clock.h"
#include "DrawGraphics.h"
#include "Theme.h"


/****************************************************************************/
/*                               E X T E R N S                              */
/****************************************************************************/

extern Calendar cal;
extern Theme thm;

/****************************************************************************/
/*             E V E N T S   :   M E M B E R   F U N C T I O N S            */
/****************************************************************************/

Events::Events() {
}

double hoursAfter12(time_t rawtime) {             // time represented as a
  struct tm * time_info = localtime(&rawtime);    // single floating-point
  return (double) (time_info->tm_hour % 12)       // hour value
       + (double) time_info->tm_min / 60
       + (double) time_info->tm_sec / 3600;
}

void
Events::populate() {
  time_t current = time(NULL);
  time_t end = current + SECS_PER_12_HOURS;

  items.clear();
  cal.getItemsForPeriod(&items, current, end);
}

void
Events::draw() {  
  list<Item>::iterator it;
    for(it = items.begin(); it != items.end(); ++it) {
      if(((Item)*it).getType() == VEVENT) {
        double start = hoursAfter12(((Item)*it).getStartTime());
        double end = hoursAfter12(((Item)*it).getEndTime());
        switch(((Item)*it).getLevel()) {
        case APPOINTMENT:
          glColor4f(thm.appointmentColor.r, thm.appointmentColor.g, thm.appointmentColor.b, thm.appointmentColor.t);
          drawArc(APPOINTMENT, start, end);
          break;
        case TASK:
          glColor4f(thm.taskColor.r, thm.taskColor.g, thm.taskColor.b, thm.taskColor.t);
          drawArc(TASK, start, end);
          break;
        case EVENT:
          glColor4f(thm.eventColor.r, thm.eventColor.g, thm.eventColor.b, thm.eventColor.t);
          drawArc(EVENT, start, end);
          break;
        case FUN_TIME:
          glColor4f(thm.funColor.r, thm.funColor.g, thm.funColor.b, thm.funColor.t);
          drawArc(FUN_TIME, start, end);
          break;
        default :	// FUN_TIME by default !
          glColor4f(thm.funColor.r, thm.funColor.g, thm.funColor.b, thm.funColor.t);
          drawArc(FUN_TIME, start, end);
          break;
        }
      }
      else { // VTODO
        double start = hoursAfter12(((Item)*it).getStartTime());
        glColor4f(thm.todoColor.r, thm.todoColor.g, thm.todoColor.b, thm.todoColor.t);
        drawNode(start);
      }
    }
}
